/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.core.model;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

public abstract class Parameter
{
	private String value;

	public Parameter(String value) {
		this.value = value;
	}
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	/**
	 * Description          : This method is able to return only the first value
	 * from the input object
	 *  
	 * @param Input input   : An input object
	 * @return String value : The first parameter value in Input object
	 */
	public String getFirstValue(Input input)
	{
		Vector<String> values = (Vector<String>) getParameterValue(input);
		return values.get(0);
	}
	
	/**
	 * Description        : This method is used to returns a colletion of strings
	 * recovered from the parameter values localized into the input object
	 * 
	 * @param Input input : An input object
	 * @return String     : A collection of values in string format
	 */
	public Collection<String> getParameterValue(Input input)
	{
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		Collection<String> values = new Vector<String>();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterValue(parameterObj))
			{
				Value value = (Value) parameterObj;
				values.add(value.getValue());
			}
		}
		return values;
	}
	
	
	/**
	 * Description         : This method is used to returns a colletion recovered
	 * variable localized into the input object
	 * 
	 * @param  Input input : An input object
	 * @return String      : A collection of variable objects
	 */
	public static Collection<Variable> getParameterVariable(Input input)
	{
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		Collection<Variable> variables = new Vector<Variable>();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterVariable(parameterObj))
				variables.add((Variable) parameterObj);
		}
		return variables;
	}
	
	/**
	 * Description         : This method is used to returns a colletion recovered
	 * file objects localized into the input object
	 * 
	 * @param  Input input : An input object
	 * @return String      : A collection of file objects
	 */
	public static Collection<File> getParameterFile(Input input)
	{
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		Collection<File> variables = new Vector<File>();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterFile(parameterObj))
				variables.add((File) parameterObj);
		}
		return variables;
	}
	
	/**
	 * Description             : This method verifies if a parameter is a value
	 * objet
	 * 
	 * @param Object parameter : A parameter object  
	 * @return boolean         : True if the object is a value object
	 */
	public boolean isParameterValue(Object parameter)
	{
		boolean result = false;
		Value valueParameter = new Value(null);
		if (parameter.getClass() == valueParameter.getClass())
			result = true;
		return result;
	}
	
	/**
	 * Description             : This method verifies if a parameter is a variable
	 * objet
	 * 
	 * @param Object parameter : A parameter object  
	 * @return boolean         : True if the object is a variable object
	 */
	public static boolean isParameterVariable(Object parameter)
	{
		boolean result = false;
		Variable variable = new Variable(null);
		if (parameter.getClass() == variable.getClass())
			result = true;
		return result;
	}
	
	/**
	 * Description             : This method verifies if a parameter is a file
	 * objet
	 * 
	 * @param Object parameter : A parameter object  
	 * @return boolean         : True if the object is a file object
	 */
	public static boolean isParameterFile(Object parameter)
	{
		boolean result = false;
		File file = new File(null);
		if (parameter.getClass() == file.getClass())
			result = true;
		return result;
	}
	
	/**
	 * Description          : This method is verifies if exists variables objets 
	 * into an input object
	 * 
	 * @param  Input input  : An input object
	 * @return boolean      : True if exists a variable
	 */
	public static boolean existsVariables(Input input)
	{
		boolean result = false;
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterVariable(parameterObj))
				result = true;
		}
		return result;
	}
	
	/**
	 * Description          : This method is verifies if exists files objets 
	 * into an input object
	 * 
	 * @param  Input input  : An input object
	 * @return boolean      : True if exists a file object
	 */
	public static boolean existsFiles(Input input)
	{
		boolean result = false;
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterFile(parameterObj))
				result = true;
		}
		return result;
	}
	
	/**
	 * Description        : This method is responsible for recover all values
	 * (variables or values objets) from input objetc
	 * 
	 * @param Input input : An input object 
	 * @return Collection : A collection of string values
	 */
	public Collection<String> getValues(Input input)
	{
		Collection<Parameter> parameters = input.getParameters();
		Iterator<Parameter> parameterIterator = parameters.iterator();
		Collection<String> values = new Vector<String>();
		while (parameterIterator.hasNext())
		{
			Object parameterObj = parameterIterator.next();
			if (isParameterValue(parameterObj))
			{
				Value value = (Value) parameterObj;
				values.add(value.getValue());
			}
			else
			{
				if (isParameterVariable(parameterObj))
				{
					Variable variable = (Variable) parameterObj;
					values.add(variable.getValue());
				}
				else
				{
					File file = (File) parameterObj;
					values.add(file.getValue());
				}
			}
		}
		return values;
	}
	
}